import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'
Vue.use(VueRouter)

const routeList = JSON.parse(store.getters.menu)
const routes = [
    {
        path: '/login',
        name: 'login',
        meta: {
            title:'登录'
        },
        component: () =>
            import('../views/login'),
    },
    {
    path: '/',
    name: 'task',
    redirect: '/task',
    meta: {
        title: '后台管理'
    },
    component: () =>
        import ('../layout/backstage'),
    children: [{
        path: '/task',
        name: 'task',
        meta: {
            title: '任务管理'
        },
        component: () =>
            import('../views/systemManage/task'),
    },
    {
        path: '/taskDetails',
        name: 'taskDetails',
        meta: {
            title: '任务详情'
        },
        component: () =>
            import ('../views/systemManage/taskDetails')
        },
        {
            path: '/onDoing',
            name: 'ondoing',
            meta: {
                title: '进行中'
            },
            component: () =>
                import ('../views/systemManage/task/onDoing')
        },
        {
            path: '/done',
            name: 'done',
            meta: {
                title: '已完成'
            },
            component: () =>
                import ('../views/systemManage/task/done')
        },
        {
            path: '/checked',
            name: 'checked',
            meta: {
                title: '已验收'
            },
            component: () =>
                import ('../views/systemManage/task/checked')
        },
    {
            path: '/mange',
            name: 'mange',
            meta: {
                title: '系统管理'
            },
            component: () =>
              import ('../layout/backstage/layout.vue'),
            children: [
                {
                    path: '/role',
                    name: 'role',
                    meta: {
                        title: '角色'
                    },
                    component: () =>
                        import('../views/systemManage/mange/role'),
                },
                {
                    path: '/page',
                    name: 'page',
                    meta: {
                        title: '用户权限'
                    },
                    component: () =>
                        import('../views/systemManage/mange/page'),
                },
                {
                    path: '/project',
                    name: 'project',
                    meta: {
                        title: '项目管理'
                    },
                    component: () =>
                        import('../views/systemManage/mange/project'),
                },
                {
                    path: '/work',
                    name: 'work',
                    meta: {
                        title: '加班管理'
                    },
                    component: () =>
                        import('../views/systemManage/mange/work'),
                }
            ]
    },
    {
        path: '/version',
        name: 'version',
        meta: {
            title: '版本管理'
        },
        component: () =>
          import ('../layout/backstage/layout.vue'),
        children: [
            {
                path: '/versionRecord',
                name: 'versionRecord',
                meta: {
                    title: '发版记录'
                },
                component: () =>
                    import('../views/systemManage/versionMange/versionRecord'),
            },
        ]
}
    ]
}, ]

const router = new VueRouter({
    routes
})
NProgress.configure({
    easing: 'ease', // 动画方式
    speed: 500, // 递增进度条的速度
    showSpinner: false, // 是否显示加载ico
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3 // 初始化时的最小百分比
})

router.beforeEach((to, from, next) => {
    // 路由发生变化修改页面title
    if (to.meta.title) {
        document.title = to.meta.title + '-TGHY-BUG平台'
    }
    
    if(!window.sessionStorage.getItem('token')){
        if(to.path !== '/login') {
            next('/login')
        }
    }
    next()
})
router.afterEach(() => {
    // 在即将进入新的页面组件前，关闭掉进度条
    NProgress.done()
})
export default router